package starlingui.state
{
	import appAdater.AppAdater;
	import appAdater.IAppAdater;
	
	import application.pictureView.PictureView;
	
	import com.imcrl.core.state.Scene;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.ui.Keyboard;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.events.KeyboardEvent;
	import starling.text.BitmapFont;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.HAlign;
	
	import starlingui.component.drag.AbsIcon;
	import starlingui.component.event.DesktopEvent;
	import starlingui.manager.TextureResource;
	import starlingui.vo.IconVO;
	
	public class SinglerScene extends Scene
	{
		public static const NAME:String="SinglerScene";
		
		public var app:IconVO;
		
		private var background:AbsIcon;
		
		private var loader:Loader;
		private var loaderContext:LoaderContext;
		public function SinglerScene()
		{
			super(NAME);
		}
		
		override public function initialize():void{
			super.initialize();
			
			var texture:Texture=TextureResource.getEmpty(Application.WIDTH,Application.HEIGHT);
			background=new AbsIcon(texture);			
			background.x=texture.width>>1;
			background.y=texture.height>>1;
			background.load("assets/defaultApp.JPG");
			this.addChild(background);
			
			loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete);
			loaderContext=new LoaderContext(false,new ApplicationDomain);
		}
		
		private var content:IAppAdater;
		
		private function complete(event:Event):void{
			var target:LoaderInfo=event.target as LoaderInfo;
			var targetContent:flash.display.DisplayObject=target.content;
			
			
			if(targetContent is IAppAdater){
				content=targetContent as IAppAdater;
			}else{
				content=new AppAdater(targetContent);
			}
			
			//test;
			content=new PictureView();
			
			
			if(content is flash.display.DisplayObject){
				var dis:flash.display.DisplayObject=content as flash.display.DisplayObject;
				point=getLeftAndtop();
				dis.x=point.x;
				dis.y=point.y;
				dis.scrollRect=new Rectangle(0,0,Application.WIDTH,Application.HEIGHT);
				Starling.current.nativeOverlay.addChild(dis);
				
				dis.alpha=0;
				tween.reset(dis,0.5);
				tween.fadeTo(1);
				Starling.current.juggler.add(tween);
			}else if(content is starling.display.DisplayObject){
				var disS:starling.display.DisplayObject=content as starling.display.DisplayObject;
				this.addChild(disS);
				
				disS.alpha=0;
				tween.reset(disS,0.5);
				tween.fadeTo(1);
				Starling.current.juggler.add(tween);
			}
			content.start();
			
			
			
			this.stage.addEventListener(Event.RESIZE,resizeHandle);
		}
		private var tween:Tween=new Tween(null,0);
		
		private function resizeHandle(event:*):void{
			if(content is flash.display.DisplayObject){
				var dis:flash.display.DisplayObject=content as flash.display.DisplayObject;
				point=getLeftAndtop();
				dis.x=point.x;
				dis.y=point.y;
			}
		}
		
		private var point:Point=new Point();
		public function getLeftAndtop():Point{
			point.x=point.y=0;
			return this.localToGlobal(point);
		}
		
		
		override public function start():void{
			clearTimeout(endInterval);
			
			if(background.state){
				background.scaleX=background.scaleY=0.1;
				background.alpha=.3;
				background.tweenPropertys(["scaleX","scaleY","alpha"],[1,1,1]);
			}
			loader.load(new URLRequest(app.app),loaderContext);
			
			this.addEventListener(DesktopEvent.EXIT,forceEnd);
		}
		
		private var endInterval:int;
		override public function end():void{
			this.stage.removeEventListener(Event.RESIZE,resizeHandle);
			
			loader.unloadAndStop(true);
			if(content){
				content.unload();
				if(content is flash.display.DisplayObject)
				{
					Starling.current.nativeOverlay.removeChild(content as flash.display.DisplayObject);
				}else if(content is starling.display.DisplayObject){
					this.removeChild(content as starling.display.DisplayObject);
				}
				content=null;
			}
			background.tweenPropertys(["scaleX","scaleY","alpha"],[.1,.1,0]);
			endInterval=setTimeout(super.end,300);
		}
		
		
		public function forceEnd(event:*):void{
			this.removeEventListener(DesktopEvent.EXIT,forceEnd);
			if(content)content.pause();
			_machine.state=DesktopScene.NAME;
			this.parent.setChildIndex(this,this.parent.numChildren-1);
		}
		
		
		private var mTextField:TextField;
		public function debug(value:String):void{
			if(mTextField==null){
				var w:int=500;
				var h:int=32;
				mTextField = new TextField(w, h, "", BitmapFont.MINI, BitmapFont.NATIVE_SIZE, 0x0);
				mTextField.fontSize=22;
				mTextField.y=(Application.HEIGHT)>>1;
				mTextField.x=(Application.WIDTH-w)>>1
				mTextField.hAlign=HAlign.CENTER;
				this.addChild(mTextField);
			}
			mTextField.text=value;
		}
		
	}
}